package cn.xfdzx.android.apps.shop.presenter;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ShopInDetailsBeen;
import cn.xfdzx.android.apps.shop.contract.AddAddressContract;
import cn.xfdzx.android.apps.shop.contract.ShopManagerContract;
import cn.xfdzx.android.apps.shop.model.AddAddressModel;
import cn.xfdzx.android.apps.shop.model.ShopManagerModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ShopManagerPresenter extends BasePresenter<ShopManagerContract.View> implements ShopManagerContract.Presenter {

    private ShopManagerContract.Model model;

    public ShopManagerPresenter() {
        model = new ShopManagerModel();
    }



    @Override
    public void anchorApply() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.anchorApply()
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onAnchorApplySuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateBusinessInfo(Map<String,Object> map,String type) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.updateBusinessInfo(map,type)
                .compose(RxScheduler.<ShopInDetailsBeen>Flo_io_main())
                .as(mView.<ShopInDetailsBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopInDetailsBeen>() {
                    @Override
                    public void accept(ShopInDetailsBeen bean) throws Exception {
                        mView.onupdateBusinessInfoSuccess(bean,type);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
